using System.Collections.Generic;
using UnityEngine;
using System.IO;

namespace FrameworkEditor
{
    public static class FileUtils
    {
        public static string TypeDefineFilePath
        {
            get { return EmmyLuaDefinePath + "EmmyTypeDefine.lua"; }
        }
        public static string LuaGlobalVariableFilePath
        {
            get { return EmmyLuaDefinePath + "/ExportTypeGlobalVariables.lua"; }
        }
        public static string EmmyLuaDefinePath
        {
            get { return Application.dataPath + "/Proj/Scripts/Lua/Editor/EmmyTypeGenerator/"; }
        }
        /// <summary>
        /// PB 文件路径
        /// </summary>
        public static string PbDefinePath
        {
            get { return Application.dataPath + "/../../../projm-server/projM-server-game/generate/proto"; }
        }

        /// <summary>
        /// 生成pb lua 辅助文件路径
        /// </summary>
        public static string PbEmmyLuaSavePath
        {
            get { return EmmyLuaDefinePath + "/PB/"; }
        }
        /// <summary>
        /// 判断文件是否存在
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>文件是否存在</returns>
        public static bool FileExists(string filePath)
        {
            return File.Exists(filePath);
        }

        /// <summary>
        /// 判断路径是否存在
        /// </summary>
        /// <param name="directoryPath">目录路径</param>
        /// <returns>路径是否存在</returns>
        public static bool DirectoryExists(string directoryPath)
        {
            return Directory.Exists(directoryPath);
        }

        /// <summary>
        /// 创建不存在的文件
        /// </summary>
        /// <param name="filePath">文件路径</param>
        public static void CreateFileIfNotExists(string filePath)
        {
            if (!FileExists(filePath))
            {
                // 确保路径存在
                string directoryPath = Path.GetDirectoryName(filePath);
                if (!string.IsNullOrEmpty(directoryPath) && !DirectoryExists(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);
                }
                // 创建文件
                File.Create(filePath).Close();
            }
        }

        /// <summary>
        /// 创建不存在的路径
        /// </summary>
        /// <param name="directoryPath">目录路径</param>
        public static void CreateDirectoryIfNotExists(string directoryPath)
        {
            if (!DirectoryExists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }
        }
        
        /// <summary>
        /// 查找指定目录下的所有.lua文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="luaFiles"></param>
        public static void FindFilesInDirectory(string path, List<string> files, string extension)
        {
            try
            {
                // 查找当前目录下的所有.lua文件
                foreach (string file in Directory.GetFiles(path, extension))
                {
                    files.Add(file);
                }
                // 递归查找子目录中的.lua文件
                foreach (string directory in Directory.GetDirectories(path))
                {
                    FindFilesInDirectory(directory, files, extension);
                }
            }
            catch (System.Exception ex)
            {
                Debug.LogError($"Error accessing {path}: {ex.Message}");
            }
        }
    }

}